/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ControlGui;

import Chiabai_total.DieuKhien;
import Chiabai_total.OpenCard;
import Chiabai_total.RotatedJLabel;
import Chiabai_total.ThreadImages;
import Chiabai_total.ThreadMain;
import Line.Bullet;
import heartnew.Board;
import heartnew.Card;
import heartnew.Player;
import java.awt.Point;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import heartnew.Card;

/**
 *
 * @author Oho
 */
public class DrawCard {

    private RotatedJLabel temp;
    int num;//so thu tu nguoi choi
    JFrame f;
    ArrayList<RotatedJLabel> rbt = new ArrayList<RotatedJLabel>();//danh sach 52 la bai tren giao dien
    int point;
    ArrayList<RotatedJLabel> changeRight = new ArrayList<RotatedJLabel>();//3 danh sach la bai hoan doi phai 
    ArrayList<RotatedJLabel> changeLeft = new ArrayList<RotatedJLabel>();//danh sach hoan goi left chua dung
    ArrayList<RotatedJLabel> changeTop = new ArrayList<RotatedJLabel>();//danh sach la bai tren hoan doi (chua dung)
    ArrayList<RotatedJLabel> changeBottom = new ArrayList<RotatedJLabel>();//danh sach la bai duoi dang giu 
    private ArrayList<Card> arr;//danh sach la bai cua nguoi choi 13 la bai 

    public DrawCard(JFrame f) {
        this.f = f;
        num = 0;
    }
    ArrayList<RotatedJLabel> CardLeft = new ArrayList<>();//danh sach la bai trai dang giu giao dien 
    ArrayList<RotatedJLabel> CardTop = new ArrayList<>();//chua dung
    ArrayList<RotatedJLabel> CardRight = new ArrayList<>();//danh sach la bai r doi cho b
    ArrayList<RotatedJLabel> CardButtom = new ArrayList<>();//
    ArrayList<RotatedJLabel> cardWin = new ArrayList<>();//danh sach la bai da thang dung de tinh toan AI 
    //cardWin danh sach la bai nho 

    public void Init() {
        for (int i = 0; i < 52; i++) {
            final RotatedJLabel lb1 = new RotatedJLabel(new ImageIcon("C:/abc/" + "pp" + ".jpg"));

            if (i < 13) {
                lb1.addMouseListener(new java.awt.event.MouseAdapter() {
                    public void mouseClicked(java.awt.event.MouseEvent evt) {
                        setTemp(lb1);
                        lb1.setBounds(lb1.getX(), lb1.getY() - 50, 100, 100);
                        changeBottom.add(lb1);
                    }
                });
                CardButtom.add(lb1);
            } else if (i < 26) {
                CardRight.add(lb1);
            } else if (i < 39) {
                CardTop.add(lb1);
            } else {
                CardLeft.add(lb1);
            }
            rbt.add(lb1);
            f.add(lb1);
            lb1.setBounds(750, 250, 100, 100);
            //if(i<13)
            lb1.setRotation(0);
        }
    }

    public void InitCard() {

        //    arr = b.getListplayer().get(num).getCardinhand();
        Init();

    }

    public void PlayCard() {
        // DieuKhien dk;
        //    ArrayList<ThreadMain> tmain = new ArrayList<>();
        for (int i = 1; i <= 4; i++) {
            int temp = 1;
            ArrayList<ThreadImages> arrimg = new ArrayList<ThreadImages>();
            for (int j = (i - 1) * 13; j < 13 * i; j++) {
                ThreadImages img;
                if (i == ThreadImages.LEFT || i == ThreadImages.RIGHT) {
                    RotatedJLabel lb = rbt.get(j);
                    lb.setRotation(90);
                    img = new ThreadImages(lb, 50, (temp * (f.getX() + 600)), i);
                } else {
                    img = new ThreadImages(rbt.get(j), (temp * (f.getX() + 600)), 50, i);
                }
                temp++;
                arrimg.add(img);
                //arrimg.add(img);
            }
            ThreadMain tim = new ThreadMain(arrimg);
            tim.start();
        }
        Sort();
        // OpenCard();
        OpenCard op = new OpenCard(getArr(), CardButtom);
        op.start();
        //op.start();

        // OpenCard();

        //tmain.get(0).start(); 
        /// dk = new DieuKhien(tmain);
        // dk.start();
        //  dk.start();
        //tmain.start();

        //dk = new DieuKhien(tmain);
        //dk.start();

    }
//    ArrayList<RotatedJLabel> CardPlayer = new ArrayList<>();

    public void pushCardTop(int position, Card c) {
        pushCards(position, CardTop, ThreadImages.MOVETOP, c);
    }

    public void pushCardRight(int position, Card c) {
        pushCards(position, CardRight, ThreadImages.MOVERIGHT, c);
    }

    public void pushCardLeft(int position, Card c) {
        pushCards(position, CardLeft, ThreadImages.MOVELEFT, c);
    }

    public void pushCards(int position, ArrayList<RotatedJLabel> arrTemp, int type, Card c) {
        try {
            RotatedJLabel tCard = arrTemp.get(position);
            //  temp = CardButtom.get(position)
            RotatedJLabel rt = new RotatedJLabel(new ImageIcon("C:/abc/" + c.getNumber() + "." + c.getRank() + ".jpg"));
            rt.setBounds(arrTemp.get(position).getX(), arrTemp.get(position).getY(), 100, 100);

            //      Point p = new Point(tCard.getX(), tCard.getY());
            cardWin.add(rt);
            f.add(rt);
            if (type == ThreadImages.MOVETOP) {
                new Bullet(rt, rt.getX(), rt.getY(), 800, 200).start();
            }
            if (type == ThreadImages.MOVELEFT) {
                new Bullet(rt, rt.getX(), rt.getY(), 700, 300).start();
            }
            if (type == ThreadImages.MOVERIGHT) {
                new Bullet(rt, rt.getX(), rt.getY(), 900, 300).start();
            }

            for (int i = position; i < arrTemp.size() - 1; i++) {
                arrTemp.get(i).setIcon(arrTemp.get(i + 1).getIcon());
            }
            f.remove(arrTemp.get(arrTemp.size() - 1));
            arrTemp.remove(arrTemp.size() - 1);
            f.validate();
            f.repaint();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }

//        arr.remove(c);
    }

    public void pushCardButtom(int position) {

        //  temp = CardButtom.get(position)
        RotatedJLabel rt = new RotatedJLabel(getTemp().getIcon());
        rt.setBounds(CardButtom.get(position).getX(), CardButtom.get(position).getY(), 100, 100);
        Point p = new Point(getTemp().getX(), getTemp().getY() + 50);
        cardWin.add(rt);
        Card c = arr.get(position);

        f.add(rt);
        new Bullet(rt, rt.getX(), rt.getY(), 800, 300).start();

//        CardButtom.remove(temp);
        for (int i = position; i < CardButtom.size() - 1; i++) {
            CardButtom.get(i).setIcon(CardButtom.get(i + 1).getIcon());
            CardButtom.get(i).setRank(CardButtom.get(i + 1).getRank());
            CardButtom.get(i).setNumber(CardButtom.get(i + 1).getNumber());
            //    CardButtom.get(i).setBounds(CardButtom.get(i+1).getX(),CardButtom.get(i+1).getY(),100,100);


        }
        CardButtom.get(position).setBounds(p.x, p.y, 100, 100);
        f.remove(CardButtom.get(CardButtom.size() - 1));
        //  ;
        arr.remove(c);
        CardButtom.remove(CardButtom.size() - 1);
        f.validate();
        f.repaint();

    }

    public void OpenCard() {
        int i = 0;
        for (i = 0; i < 13; i++) {
            getArr().get(i).getNumber();
            Icon icon = new ImageIcon("c:/abc/" + getArr().get(i).getNumber() + "." + getArr().get(i).getRank() + ".jpg");
            final RotatedJLabel rt = rbt.get(i);
            rt.setIcon(icon);


        }
    }

    public void ResetCard() {
        for (RotatedJLabel rt1 : rbt) {
            ImageIcon img = new ImageIcon("C:/abc/" + "pp" + ".jpg");
            rt1.setRotation(0);
            rt1.setBounds(800, 300, 100, 100);
            rt1.setIcon(img);
        }
    }

    public void Sort() {

        Collections.sort(getArr(), new AscCard());

    }

    public void ShowInfo() {
        //JOptionPane.showMessageDialog(temp, "number =" + temp.getNumber()+";"+"rank = "+temp.getRank());
        for (int i = 0; i < CardButtom.size(); i++) {
            if (getTemp() == CardButtom.get(i)) {
                pushCardButtom(i);

                break;
            }
        }



    }

    public void AcessWinButtom() {
        for (RotatedJLabel rt : cardWin) {
            new ThreadImages(rt, 800, 600, ThreadImages.MOVETOP).start();
            //new Bullet(rt, rt.getX(), ..., 800, 600,1,this).start();
        }
    }

    public void AcessWinRight() {
        for (RotatedJLabel rt : cardWin) {
            new Bullet(rt, rt.getX(), rt.getY(), 1200, 300).start();
        }
    }

    public void AcessWinTop() {
        for (RotatedJLabel rt : cardWin) {
            new Bullet(rt, rt.getX(), rt.getY(), 800, 100).start();

        }
    }

    public void AcessWinLeft() {
        for (RotatedJLabel rt : cardWin) {
            new Bullet(rt, rt.getX(), rt.getY(), 0, 300).start();

        }
    }

    public void changleCards() {
        int i = 0;
        for (RotatedJLabel temp : changeBottom) {
            //  temp = CardButtom.get(position)
            RotatedJLabel rt = changeRight.get(i);
            temp.setIcon(rt.getIcon());
        }
//        for (int i = position; i < arr.size() - 1; i++) {
//            arr.get(i).setIcon(arr.get(i + 1).getIcon());
//            //CardTop.get(i).setBounds(CardTop.get(i+1).getX(),CardTop.get(i+1).getY(),100,100);
//            //CardTop.get(i).setRank(CardTop.get(i + 1).getRank());
//            //CardTop.get(i).setNumber(CardTop.get(i + 1).getNumber());
//            //   CardButtom.get(i).setBounds(CardButtom.get(i+1).getX(),CardButtom.get(i+1).getY(),100,100);
//        }
//     //   CardTop.get(position).setBounds(p.x, p.y, 100, 100);
//        f.remove(arr.get(arr.size() - 1));
//
//        f.validate();
//        f.repaint();
//        arr.remove(arr.size() - 1);

        //      }
    }

    /**
     * @return the arr
     */
    public ArrayList<Card> getArr() {
        return arr;
    }

    /**
     * @param arr the arr to set
     */
    public void setArr(ArrayList<Card> arr) {
        this.arr = arr;
    }

    /**
     * @return the temp
     */
    public RotatedJLabel getTemp() {
        return temp;
    }

    /**
     * @param temp the temp to set
     */
    public void setTemp(RotatedJLabel temp) {
        this.temp = temp;
    }
}
